﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SAUlot
{
    public partial class AirplaneMainView : Form
    {
        private bool thread_started;

        private AirplaneControler airplaneControler;
        public AirplaneControler Controler
        {
            get { return airplaneControler; }
            set { airplaneControler = value; SetSpinners(); }
        }

        private RaportForm raportForm;

        public RaportForm Raport
        { 
            get { return raportForm; } 
        }

        public AirplaneMainView()
        {
            InitializeComponent();
            raportForm = new RaportForm();
            raportForm.Hide();
            thread_started = false;
        }

        public Size ViewportSize
        {
            get { return canvas.Size; }
        }

        public IntPtr CanvasHandle
        {
            get { return canvas.Handle; }
        }

        private void raportButton_Click(object sender, EventArgs e)
        {
            raportForm.Show();
        }

        private void startStopButton_Click(object sender, EventArgs e)
        {
            if (!thread_started)
            {
                startStopButton.Text = "Stop";
                thread_started = true;
                AKButton.Enabled = false;
                AKrepeatButton.Enabled = false;
                radioButton1.Enabled = false;
                radioButton2.Enabled = false;
                resetButton.Enabled = false;
                actorBetaSpinner.Enabled = false;
                criticBetaSpinner.Enabled = false;
                discountSpinner.Enabled = false;
                deviationSpinner.Enabled = false;
                airplaneControler.StartThread();
            }
            else
            {
                startStopButton.Text = "Start";
                AKButton.Enabled = true;
                AKrepeatButton.Enabled = true;
                radioButton1.Enabled = true;
                radioButton2.Enabled = true;
                resetButton.Enabled = true;
                actorBetaSpinner.Enabled = true;
                criticBetaSpinner.Enabled = true;
                discountSpinner.Enabled = true;
                deviationSpinner.Enabled = true;
                airplaneControler.EndThread();
                thread_started = false;
            }
        }
        
        private void AirplaneMainView_FormClosing(object sender, FormClosedEventArgs e)
        {
            airplaneControler.EndThread();
            Application.Exit();
        }

        private void resetButton_Click(object sender, EventArgs e)
        {
            if (thread_started)
            {
                thread_started = false;
                airplaneControler.EndThread();
                startStopButton.Text = "Start";
            }
            airplaneControler.Reset();

         }

        private void AKButton_CheckedChanged(object sender, EventArgs e)
        {
            if (thread_started)
            {
                thread_started = false;
                airplaneControler.EndThread();
                startStopButton.Text = "Start";
            }
            if (AKButton.Checked)
                airplaneControler.SetActorCritic = 0;
            else
                airplaneControler.SetActorCritic = 1;
            SetSpinners();
        }

        private void AKrepeatButton_CheckedChanged(object sender, EventArgs e)
        {
            if (thread_started)
            {
                thread_started = false;
                airplaneControler.EndThread();
                startStopButton.Text = "Start";
            }
            if (AKrepeatButton.Checked)
                airplaneControler.SetActorCritic = 1;
            else
                airplaneControler.SetActorCritic = 0;
            
            SetSpinners();
        }

        private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {
            if (thread_started)
            {
                thread_started = false;
                airplaneControler.EndThread();
                startStopButton.Text = "Start";
            }
            if (radioButton1.Checked)
                airplaneControler.SetVariableNumber = 0;
            else
                airplaneControler.SetVariableNumber = 1;

            SetSpinners();
        }

        private void radioButton2_CheckedChanged(object sender, EventArgs e)
        {
            if (thread_started)
            {
                thread_started = false;
                airplaneControler.EndThread();
                startStopButton.Text = "Start";
            }
            if (radioButton2.Checked)
                airplaneControler.SetVariableNumber = 1;
            else
                airplaneControler.SetVariableNumber = 0;

            SetSpinners();
        }

        private void SetSpinners()
        {
            criticBetaSpinner.Value = (decimal)airplaneControler.AlgorithmBetaV;
            actorBetaSpinner.Value = (decimal)airplaneControler.AlgorithmBetaFi;
            discountSpinner.Value = (decimal)airplaneControler.AlgorithmGamma;
            deviationSpinner.Value = (decimal)airplaneControler.AlgorithmDeviation;
        }

        private void criticBetaSpinner_ValueChanged(object sender, EventArgs e)
        {
            airplaneControler.AlgorithmBetaV =(double) criticBetaSpinner.Value;
        }

        private void actorBetaSpinner_ValueChanged(object sender, EventArgs e)
        {
            airplaneControler.AlgorithmBetaFi =(double) actorBetaSpinner.Value;
        }

        private void discountSpinner_ValueChanged(object sender, EventArgs e)
        {
            airplaneControler.AlgorithmGamma = (double)discountSpinner.Value;
        }

        private void deviationSpinner_ValueChanged(object sender, EventArgs e)
        {
            airplaneControler.AlgorithmDeviation = (double)deviationSpinner.Value;
        }

      
   }
}

